# -*- coding: utf-8 -*-

# -----------------------------------------------------------------------------
# _path is only needed if run from the examples directory
try:
    import _path # has to be first import to make sure it finds the 'context' package
except:
    pass
# -----------------------------------------------------------------------------

import random

import pygame

# import context
import pyknicpygame
from pyknicpygame import pyknic

pyknicpygame.init()

WINDOW_SIZE = (800, 600)
DURATION = 1.0

class EffectShowContext(pyknicpygame.pyknic.context.Context):

    effects = [
                pyknicpygame.context.effects.Slide(DURATION, WINDOW_SIZE[0], pyknicpygame.context.effects.Slide.LEFT),
                pyknicpygame.context.effects.Slide(DURATION, WINDOW_SIZE[0], pyknicpygame.context.effects.Slide.RIGHT),
                pyknicpygame.context.effects.Slide(DURATION, WINDOW_SIZE[1], pyknicpygame.context.effects.Slide.UP),
                pyknicpygame.context.effects.Slide(DURATION, WINDOW_SIZE[1], pyknicpygame.context.effects.Slide.DOWN),
                pyknicpygame.context.effects.Slide(DURATION, WINDOW_SIZE[0], pyknicpygame.context.effects.Slide.LEFT, False),
                pyknicpygame.context.effects.Slide(DURATION, WINDOW_SIZE[0], pyknicpygame.context.effects.Slide.RIGHT, False),
                pyknicpygame.context.effects.Slide(DURATION, WINDOW_SIZE[1], pyknicpygame.context.effects.Slide.UP, False),
                pyknicpygame.context.effects.Slide(DURATION, WINDOW_SIZE[1], pyknicpygame.context.effects.Slide.DOWN, False),
                pyknicpygame.context.effects.Slide(DURATION, WINDOW_SIZE[0], pyknicpygame.context.effects.Slide.LEFT, True, False),
                pyknicpygame.context.effects.Slide(DURATION, WINDOW_SIZE[0], pyknicpygame.context.effects.Slide.RIGHT, True, False),
                pyknicpygame.context.effects.Slide(DURATION, WINDOW_SIZE[1], pyknicpygame.context.effects.Slide.UP, True, False),
                pyknicpygame.context.effects.Slide(DURATION, WINDOW_SIZE[1], pyknicpygame.context.effects.Slide.DOWN, True, False),
                pyknicpygame.context.effects.FadeOutEffect(DURATION),
                pyknicpygame.context.effects.FadeOutEffect(DURATION),
              ]
    current_effect = -1
    old_think = False
    new_think = False
    font = None
    font_small = None

    def __init__(self):
        pyknicpygame.pyknic.context.Context.__init__(self)
        self.color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
        self.text = ""
        self.trans_description = ""

    def exit(self):
        # clear events after transition
        pygame.event.clear()

    def enter(self):
        EffectShowContext.current_effect += 1
        if EffectShowContext.current_effect >= len(EffectShowContext.effects):
            EffectShowContext.current_effect = 0
        self.text = "Context B" if self.current_effect % 2 else "Context A"
        self.trans_description = str(EffectShowContext.effects[EffectShowContext.current_effect])
        
    def think(self, dt):
        pyknicpygame.pyknic.context.Context.think(self, dt) # why does that create a recursion depth exception?
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pyknicpygame.pyknic.context.pop()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    pyknicpygame.pyknic.context.pop()
                elif event.key == pygame.K_SPACE:
                    transEffect = EffectShowContext.effects[EffectShowContext.current_effect]
                    pyknicpygame.pyknic.context.push( \
                        pyknicpygame.pyknic.context.transitions.Transition( EffectShowContext(), 
                                                        transEffect, 
                                                        EffectShowContext.old_think, 
                                                        EffectShowContext.new_think)
                                                        )
            pyknicpygame.pyknic.context.print_stack()

    def draw(self, screen):
        screen.fill(self.color)
        text_image = self.font.render(self.text, 0, (255, 255, 255))
        rect = text_image.get_rect(center=screen.get_rect().center)
        screen.blit(text_image, rect)
        
        text_image = self.font_small.render(self.trans_description, 0, (255, 255, 255))
        rect = text_image.get_rect(center=screen.get_rect().center)
        screen.blit(text_image, rect.move(0, 60))
        


def main():

    # init pygame
    pygame.init()
    screen = pygame.display.set_mode(WINDOW_SIZE)
    pygame.display.set_caption("effects demo, press 'SPACE' for next transition")
    clock = pygame.time.Clock()
    
    # init context
    con = EffectShowContext()
    pyknicpygame.pyknic.context.push(con)
    EffectShowContext.font = pygame.font.Font(None, 60)
    EffectShowContext.font_small = pygame.font.Font(None, 20)
    
    # loop
    while pyknicpygame.pyknic.context.top():
        dt = clock.tick(60.0) / 1000.0 # convert to seconds
        pyknicpygame.pyknic.context.top().draw(screen)
        pyknicpygame.pyknic.context.top().think(dt)
        pygame.display.flip()



if __name__ == "__main__":

    main()



